<?
/*
 *  GGB - Gelin's Guest Book.
 *  Copyright (C) 2006  Denis Nelubin aka Gelin
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *  http://gelin.ru
 *  mailto:den@gelin.ru
 *
 *  Description:    Formatter for list of pages.
 *
 *  History:
 *  2006-08-05  Gelin   Initially created, methods moved from Formatter.php.
 *
 */

/**
 *  Formatter for list of pages.
 */
class PagesFormatter {

    /**
     *  Creates array of links to pages.
     *  @param  $currentPage    number of current page
     *  @param  $numberOfPages  total number of pages
     *  @return array of links to pages
     */
    function preparePages($currentPage, $numberOfPages) {
        global $config;

        $pages = array();
        //link to first page
        if ($currentPage > 0) {
            $page['href'] = $this->getHref(0);
        } else {
            $page['href'] = NULL;
        }
        $pages['first'] = $page;
        //link to previous page
        if ($currentPage > 0) {
            $page['href'] = $this->getHref($currentPage - 1);
        } else {
            $page['href'] = NULL;
        }
        $pages['prev'] = $page;
        //find pages intervals
        $afterFirst = $config['pages']['after_first'];
        $nearCurrent = $config['pages']['near_current'];
        $beforeLast = $config['pages']['before_last'];
        $from = $currentPage - $nearCurrent;
        $to = $currentPage + $nearCurrent + 1;
        if ($from < 0) {
            $to += -$from;
            $from = 0;
        }
        if ($to > $numberOfPages) {
            $from -= ($to - $numberOfPages);
            $to = $numberOfPages;
        }
        if ($from < 0) {
            $from = 0;
        }
        //links to some pages after first
        for ($i = 0; $i < $afterFirst && $i < $numberOfPages; $i++) {
            $page['number'] = $i + 1;
            if ($i == $currentPage) {
                $page['href'] = NULL;
            } else {
                $page['href'] = $this->getHref($i);
            }
            $pages['pages'][$i] = $page;
        }
        //gap between groups of first and current pages
        if ($afterFirst - 1 < $from) {
            $pages['pages'][$afterFirst]['gap'] = TRUE;
        }
        //links to some current pages
        for ($i = $from; $i < $to; $i++) {
            $page['number'] = $i + 1;
            if ($i == $currentPage) {
                $page['href'] = NULL;
            } else {
                $page['href'] = $this->getHref($i);
            }
            $pages['pages'][$i] = $page;
        }
        //gap between groups of current pages and last pages
        if (($numberOfPages - $beforeLast) > $to) {
            $pages['pages'][$numberOfPages - $beforeLast - 1]['gap'] = TRUE;
        }
        //links to last pages
        for ($i = (($numberOfPages - $beforeLast) > 0) ? ($numberOfPages - $beforeLast) : 0 ;
                $i < $numberOfPages; $i++) {
            $page['number'] = $i + 1;
            if ($i == $currentPage) {
                $page['href'] = NULL;
            } else {
                $page['href'] = $this->getHref($i);
            }
            $pages['pages'][$i] = $page;
        }

        unset($page['number']);
        //link to next page
        if ($currentPage < $numberOfPages - 1) {
            $page['href'] = $this->getHref($currentPage + 1);
        } else {
            $page['href'] = NULL;
        }
        $pages['next'] = $page;
        //link to last page
        if ($currentPage < $numberOfPages - 1) {
            $page['href'] = $this->getHref($numberOfPages - 1);
        } else {
            $page['href'] = NULL;
        }
        $pages['last'] = $page;
        //set flag to current page
        $pages['pages'][$currentPage]['current'] = TRUE;

        return $pages;
    }

    /**
     *  Creates HREF for specified page
     */
    function getHref($page) {
        return '?page='.$page;
    }

}

?>